package com.cloudptt.api.product.func.call.group.fsm.floor;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;

public class FloorNoPermissionState implements ISubState, IRollback
{
	private FloorAgent agent = null;
	private String curGroupNum =  null;
	private int curPri = 0;
	//	private String name = "";
	private boolean isTaken = false;

	private Context ctx = null;
	private RollbackTimer rollbackTimer = null;
	//	private String num = null;

	public FloorNoPermissionState(Context ctx, FloorAgent agent,String groupNum,int pri)
	{
		this.ctx = ctx;
		this.agent = agent;
		this.curGroupNum = groupNum;
		this.curPri = pri;
		Log.d(FloorAgent.Log_Key, "currentState is FloorPermissionState groupNum = " + groupNum + " pri = " + pri);
		this.agent.opAudioMedia(0);
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Request == btnAction)
		{
//			if(isIdle)
//			{
			int result = agent.floorRequest();
			if(CauseCont.OperateCheck_Suc == result)
			{
				agent.goToState(CallConst.FloorState_PendingRequset,null);
			}
			return result;
//			}
//			else
//			{
//				return CauseCont.OperateErr_RightOccupy;
//			}
		}
		else if(CallConst.BtnAction_Release == btnAction)
		{
		}
		return CauseCont.OperateCheck_Suc;
	}

	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			Log.d(FloorAgent.Log_Key, "currentState is IdleState msg == null");
			return;
		}
		String callType = msg.getValue(RtcpMsg.Key_CallType);
		String newNum = msg.getValue(RtcpMsg.Key_To);
//		if(curGroupNum != null
//				&& curGroupNum.equals(groupNum)) {
		String title = msg.getTitle();
		Log.d(FloorAgent.Log_Key, "receiveRtcpMsg title = " + title);
		if (title.equals(RtcpMsg.Title_FLOOR_TAKEN)) {
			Log.d(FloorAgent.Log_Key, "receiveRtcpMsg isTaken = " + isTaken);
//			if(!isTaken)
			{
				curGroupNum = newNum;
				if(!RtcpMsg.isGroupCall(callType)){
					newNum = msg.getValue(RtcpMsg.Key_From);
				}
				String num = msg.getValue(RtcpMsg.Key_From);
				//String groupNum = msg.getValue(RtcpMsg.Key_GroupId);
				Log.d(FloorAgent.Log_Key, "receiveRtcpMsg newNum = " + newNum + " num = " + num);
				this.agent.updateSpeakPermissionNum(callType,newNum, num);

				CallTimer callTimer = agent.getCallTimer();
				int t180 = callTimer.getT180();
				rollbackTimer = new RollbackTimer(ctx, t180, this);

				isTaken = true;
			}
		} else if (title.equals(RtcpMsg.Title_FLOOR_IDLE)) {

			if(isTaken
			&& curGroupNum != null
					&& curGroupNum.equals(newNum)) {
				if (rollbackTimer != null) {
					rollbackTimer.cancelTimer();
					rollbackTimer = null;
				}

//				if (!isIdle)
				{
					if(!RtcpMsg.isGroupCall(callType)){
						newNum = msg.getValue(RtcpMsg.Key_From);
					}
					//String groupNum = msg.getValue(RtcpMsg.Key_GroupId);
					Log.d(FloorAgent.Log_Key, "receiveRtcpMsg groupNum = " + newNum);
					if(newNum != null
						&& !newNum.equals("")) {
						this.agent.updateSpeakPermissionNum(callType, newNum, "");
					}
				}
				isTaken = false;
			}
		} else if (title.equals(RtcpMsg.Title_FLOOR_GRANTED)) {
			agent.goToState(CallConst.FloorState_Permission, null);
		}
//		}
	}
	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{
		if(user != null)
		{
			String gNum = gInfo.getMcId();
			String num = user.getMcId();
			if(num != null) {
				this.agent.updateSpeakPermissionNum(callType,gNum,num);
			}
		}
	}
	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " onTimeout");
		if(agent != null)
		{
			agent.clearCurGroup();
		}
	}

	public int getState()
	{
		return CallConst.FloorState_NoPermission;
	}
	public void destory()
	{
		if(rollbackTimer != null) {
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}
//	@Override
//	public String getSpeakName() {
//		// TODO Auto-generated method stub
//		return name;
//	}
}
